<template>
<div>
    <el-row :gutter="20">
      <el-col :span="6" v-for="item in wallpaperList" :key="item.id">
        <el-card>
          <img :src="item.imgUrl" style="width: 230px;height: 180px">
          <div>
          <el-button type="primary" @click="updateWallpaper(item.id)">编辑</el-button>
          </div>
        </el-card>
      </el-col>
    </el-row>
  <!--     照片墙-->
  <el-dialog  :visible.sync="dialogEditVisible">
    <el-tag>图片上传</el-tag>
    <el-upload
      ref="upload"
      action="http://localhost:8085/upload"
      list-type="picture-card"
      :on-success="fileSuccess"
      :on-preview="handlePictureCardPreview"
      :multiple="true"
      :on-remove="handleRemove">
      <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="imgUrl" alt="">
    </el-dialog>
    <el-row>
      <el-col :offset="18">
        <el-button type="primary" @click="commit">修改</el-button>
        <el-button @click=" dialogUpdateVisible= false">取 消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</div>
</template>

<script>
import { request } from '@/network/request';
import { basePath } from '../../commom/commom';

export default {
  name: 'WallpaperList',
  data() {
    return {
      wallpaperList: [],
      dialogEditVisible: false,
      imgUrl: '',
      id: '',
      dialogVisible: false,
    };
  },
  created() {
    this.getWallpaperList();
  },
  methods: {
    getWallpaperList() {
      request({
        url: `${basePath}/wallpaper/list`,
      }).then((res) => {
        this.wallpaperList = res.data;
        for (let i = 0; i < res.data.length; i += 1) {
          this.wallpaperList[i].imgUrl = `${basePath}${res.data[i].imgUrl}`;
        }
      });
    }, // 删除数组里的照片
    handleRemove(file) {
      this.progressFormData.imgUrl = this.progressFormData.imgUrl.filter((x) => x !== file.url);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    fileSuccess(file) {
      // eslint-disable-next-line prefer-destructuring
      this.imgUrl = file.data[0];
    },
    updateWallpaper(id) {
      this.dialogEditVisible = true;
      this.id = id;
    },
    commit() {
      request({
        url: `${basePath}/wallpaper/update`,
        method: 'put',
        params: {
          id: this.id,
          imgUrl: this.imgUrl,
        },
      }).then((res) => {
        if (res.code !== 200) return this.$message.error('更新失败');
        this.getWallpaperList();
        this.$refs.upload.clearFiles();
        this.dialogEditVisible = false;
        return this.$message.success('更新成功');
      });
    },
  },
};
</script>

<style scoped>

</style>
